﻿using QuickApp.DbInterface;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace QuickApp.SqlSilver
{
    public class SqlServerConnection : QADbConnection
    {
        public SqlServerConnection(string name)
            : base(QADbType.SqlServer, name)
        {

        }
        protected override bool buildConnString()
        {
            if (!buildStringSuccess)
            {
                if (string.IsNullOrWhiteSpace(Server) || string.IsNullOrWhiteSpace(Database) || 
                    (!TrustedConnection && string.IsNullOrWhiteSpace(UserId) && string.IsNullOrWhiteSpace(Password)))
                    return false;
                StringBuilder builder = new StringBuilder();
                builder.Append(string.Format("Server={0};Database={1};",Server,Database));
                if (TrustedConnection)
                    builder.Append("Trusted_Connection=True;");
                else
                    builder.Append(string.Format("User Id={0};Password={1};",UserId,Password));
                ConnectionString = builder.ToString();
                buildStringSuccess = true;
            }
            return buildStringSuccess;
        }

        public override DbConnection getNewConnection()
        {
            return new SqlConnection(connectionString);
        }
    }
}

